package com.ganbo.ms.io;

import com.ganbo.ms.io.dto.Group;
import com.ganbo.ms.io.dto.User;
import com.ms.common.utils.ProtostuffUtils;
import com.sun.deploy.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author ganbo
 * @date 2022/11/12 8:07
 */

@Slf4j
public class FileTest {


    @Test
    public void createFileDemo() throws IOException {
        File f = new File("c://1.txt");
//        f.createNewFile();

        System.out.println(f.getName());
        System.out.println(f.getAbsoluteFile());
        System.out.println(f.getPath());
        System.out.println(f.getParentFile());
        System.out.println(f.isFile());
        System.out.println(f.length());
        System.out.println(f.exists());
//        System.out.println(f.delete());
        System.out.println("创建成功。。。。");
    }


    @Test
    public void createFileDemo1() throws IOException {
        File f = new File("c://1.txt");
        FileInputStream fio = new FileInputStream(f);
        int temp;
        byte[] arr = new byte[100];
        while ((temp = fio.read()) != -1) {
            System.out.print((char) temp);
        }
    }

    @Test
    public void createFileDemo33() throws IOException {
        File f = new File("c://666.txt");
        FileWriter fw = new FileWriter(f);
        for (int i = 0; i < 10000; i++) {
            fw.write(UUID.randomUUID().toString().replace("-", ""));
        }
        fw.close();
        System.out.println("=========完毕===");
    }


    /**
     * 每次发送最大多少条行情数据
     */
    private Integer maxTick = 2;

    /**
     * 文件中，每条记录大小（Length+Data）
     */
    private Integer binaryPacketSize = 10;

    int count = 0;

    @Test
    public void ranDomReadFileTest() throws IOException {
        RandomAccessFile accessFile = new RandomAccessFile("c://666.txt", "r");
        System.out.println(accessFile.length());
        boolean flag = false;
        while (true) {

            byte[] buffer = new byte[maxTick * binaryPacketSize];
            int num = 0;
            long retain = accessFile.length() - accessFile.getFilePointer(); //剩余多少字节长度没有读取
            long mod = retain / binaryPacketSize; //还有多少个行情数据
            if (mod >= maxTick) {
                num = maxTick;  //不是最后一次
            } else {
                num = (int) mod;   //最后一次读取
                flag = true;
            }
            count += num;  //总共读了多少条行情数据了
            log.info("总推送数据{}条", count);
            accessFile.read(buffer, 0, num * binaryPacketSize);

            String s = new String(buffer, 0, num * binaryPacketSize);
            log.info("本次推送的原始内容---->{},   length：{}", s, s.length());

            if (flag) break;
        }

        System.out.println(accessFile.length()/(binaryPacketSize));
    }


    @Test
    public void createFileDemo3() throws IOException {
        File f = new File("c://1.txt");
        FileInputStream fio = new FileInputStream(f);
        byte[] arr = new byte[3];
        int length;
        while ((length = fio.read(arr)) != -1) {
            System.out.print(new String(arr));
            System.out.println("length==========>" + length);
        }

        fio.close();

        System.out.println();
    }

    @Test
    public void createFileDemo2() throws IOException {
        int[] arr = new int[23];
        System.out.println(arr[22]);
    }

    @Test
    public void createFileDemo4() throws IOException {
        File f = new File("c://1.txt");
        FileReader fileReader = new FileReader(f);
        int c;
        while ((c = fileReader.read()) != -1) {
            System.out.print((char) c);
        }
        System.out.println();
        fileReader.close();
    }


    @Test
    public void fileOutPutStreamTest() throws IOException {

        File file = new File("c://3.txt");
        FileOutputStream fo = new FileOutputStream(file);

        String str = "hello world";
        byte[] bytes = str.getBytes();

        for (int i = 0; i < bytes.length; i++) {
            int temp = bytes[i];
            fo.write(temp);
        }

//        file.createNewFile();
        fo.close();
    }


    @Test
    public void fileCopy() throws IOException {
        File source = new File("c:\\io类结构.png");
        File target = new File("d:\\io类结构.png");
        FileInputStream fion = new FileInputStream(source);
        FileOutputStream fout = new FileOutputStream(target);

        byte[] temp = new byte[100];
        int length;
        while ((length = fion.read(temp)) != -1) {
            fout.write(temp, 0, length); //这里一定要用这个方法
        }

        System.out.println("拷贝完毕");
        fion.close();
        fout.close();
    }

    @Test
    public void fileCopy2() throws IOException {
        File source = new File("c:\\1.txt");
        File target = new File("d:\\1.txt");
        FileReader fion = new FileReader(source);
//        FileWriter fout = new FileWriter(target); //覆盖模式
        FileWriter fout = new FileWriter(target, true); //追加模式

        char[] temp = new char[100];
        int length;
        while ((length = fion.read(temp)) != -1) {
            fout.write(temp, 0, length); //这里一定要用这个方法
        }

        System.out.println("拷贝完毕");
        fion.close();
        fout.close(); //这里一定要关闭或者flush不然还是没写进去
    }

    @Test
    public void fileCopy4() throws IOException {
        File source = new File("c:\\1.txt");
        File target = new File("d:\\1.txt");
        FileReader fion = new FileReader(source);
//        FileWriter fout = new FileWriter(target); //覆盖模式
        FileWriter fout = new FileWriter(target, true); //追加模式

        BufferedReader bufferedReader = new BufferedReader(fion);
        BufferedWriter bufferedWriter = new BufferedWriter(fout);

        String temp;

        while ((temp = bufferedReader.readLine()) != null) {
            System.out.println(temp);
            bufferedWriter.write(temp);
        }


//        char[] temp = new char[100];
//        int length;
//        while ((length = fion.read(temp)) != -1) {
//            fout.write(temp, 0, length); //这里一定要用这个方法
//        }
//
//        System.out.println("拷贝完毕");


        bufferedReader.close();
        bufferedWriter.close();

//        fion.close();
//        fout.close(); //这里一定要关闭或者flush不然还是没写进去
    }


    public static String generateNewName(String name) {
//        String name = "006-尚硅谷-Netty核心技术及源码剖析-BIO实例及分析.avi";
        String substring1 = name.substring(0, name.indexOf('-'));
        String substring = name.substring(name.lastIndexOf('-') + 1);
//        System.out.println(substring1 + "-" + substring);
        return substring1 + "-" + substring;
    }


    @Test
    public void fileCopy5() throws IOException {
        File source = new File("E:\\尚硅谷全套\\尚硅谷Netty核心技术教程\\视频");
        File target = new File("E:\\尚硅谷全套\\尚硅谷Netty核心技术教程\\视频1");

        if (!target.exists()) {
            target.mkdir();
        }
        File[] files = source.listFiles();
        for (File file : files) {
            boolean b = file.getName().endsWith(".avi");
            if (b) {
                File f = new File(target, generateNewName(file.getName()));
                BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(f));

                byte[] arr = new byte[1000];
                int length;
                while ((length = bufferedInputStream.read(arr)) != -1) {
                    bufferedOutputStream.write(arr, 0, length);
                }

                bufferedInputStream.close();
                bufferedOutputStream.close();
                System.out.println(f.getName() + "拷贝完毕");
            }
        }
    }


    @Test
    public void fileCopy6() throws IOException {
        File source = new File("c:\\1.txt");
//        FileInputStream in = new FileInputStream(source);
//        InputStreamReader ir = new InputStreamReader(in);
//        BufferedReader bf = new BufferedReader(ir);

        BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream(new File("c:\\1.txt"))));


//        char[] arr = new char[30];
//        int length;
//        while ((length = ir.read(arr)) != -1) {
//            System.out.println(new String(arr, 0, length));
//        }
//        ir.close();

        String temp;
        while ((temp = bf.readLine()) != null) {
            System.out.println(temp);
        }

        bf.close();
    }


    @Test
    public void testSer() {
        //创建一个user对象
        User user = User.builder().id("1").age(20).name("张三").desc("programmer").build();
        //创建一个Group对象
        Group group = Group.builder().id("1").name("分组1").user(user).build();
        //使用ProtostuffUtils序列化
        byte[] data = ProtostuffUtils.serialize(group);
        System.out.println("序列化后：" + Arrays.toString(data));
        Group result = ProtostuffUtils.deserialize(data, Group.class);
        System.out.println("反序列化后：" + result.toString());



    }


}
